package com.bzkj.common.utils.date;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @projectName: bzkj-wms
 * @package: com.bzkj.common.utils.date
 * @className: DateUtils
 * @description: 日期处理工具类
 * @author: C1309
 * @createDate: 2024年11月18日
 * @version: v1.0
 */
import java.text.SimpleDateFormat;
import java.util.*;

public class WmsDateUtils {

    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


    /**
     * 获取当天日期并将间隔天数加减
     *
     * @param interval 间隔时间（单位：天）
     * @return 时间数组
     */
    public static List<Date> dateInterval(int interval) {
        Calendar today = Calendar.getInstance();
        Calendar beforeDate = Calendar.getInstance();

        // 设置当前时间为23:59:59
        today.set(Calendar.HOUR_OF_DAY, 23);
        today.set(Calendar.MINUTE, 59);
        today.set(Calendar.SECOND, 59);

        // 设置时间为00:00:00并根据间隔天数修改
        beforeDate.add(Calendar.DAY_OF_YEAR, interval);
        beforeDate.set(Calendar.HOUR_OF_DAY, 0);
        beforeDate.set(Calendar.MINUTE, 0);
        beforeDate.set(Calendar.SECOND, 0);

        return Arrays.asList(today.getTime(), beforeDate.getTime());
    }

    /**
     * 设置默认的日期范围
     * @param days 天数
     * @return 格式化的日期范围
     */
    public static String setDefaultDateRange(int days) {
        List<Date> dateList = dateInterval(days);
        if (dateList.size() == 2) {
            // Sort the dates to ensure correct order
            Collections.sort(dateList);

            // 获取排序后的开始时间和结束时间
            LocalDateTime startDate = dateList.get(0).toInstant()
                    .atZone(ZoneId.of("Asia/Shanghai"))
                    .toLocalDateTime();
            LocalDateTime endDate = dateList.get(1).toInstant()
                    .atZone(ZoneId.of("Asia/Shanghai"))
                    .toLocalDateTime();;

            // 返回日期范围
            return startDate.format(formatter) + "~" + endDate.format(formatter);
        } else {
            // Return an error message or handle the edge case as required
            throw new IllegalArgumentException("Invalid date interval generated.");
        }
    }
}

